package com.lzx.iteam.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.lzx.iteam.BaiduMapActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.AttendanceData;
import com.lzx.iteam.util.DateUtil;
import com.lzx.iteam.util.PreferenceUtil;
import com.lzx.iteam.util.StringUtil;

import java.util.ArrayList;

import static android.R.attr.data;

/**
 * Created by Megan on 16/4/11.
 */
public class AttendanceCreateAdapter extends BaseAdapter {


    private Context mContext;
    private ArrayList<AttendanceData> attendanceDatas;
    private PreferenceUtil mPreferenceUtil;
    private AttendanceEditListener mListener;

    public AttendanceCreateAdapter(Context mContext) {
        this.mContext = mContext;
        mPreferenceUtil = PreferenceUtil.getInstance(mContext);

    }


    public void bindData(ArrayList<AttendanceData> attendanceDatas) {
        this.attendanceDatas = attendanceDatas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return attendanceDatas == null ? 0 : attendanceDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return attendanceDatas == null ? null : attendanceDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        ViewHolder holder;
        final AttendanceData attendanceData = attendanceDatas.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.attendance_create_list_item_layout, null);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.tv_attendance_create_list_name);
            holder.workDate = (TextView) convertView.findViewById(R.id.tv_attendance_create_list_work_date);
            holder.startTime = (TextView) convertView.findViewById(R.id.tv_attendance_create_list_start_time);
            holder.endTime = (TextView) convertView.findViewById(R.id.tv_attendance_create_list_end_time);
            holder.updateNote = (TextView) convertView.findViewById(R.id.tv_attendance_create_list_update_note);
            holder.editRule = (TextView) convertView.findViewById(R.id.tv_attendance_create_edit_rule);
            holder.editSub = (TextView) convertView.findViewById(R.id.tv_attendance_create_edit_sub);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.name.setText(attendanceData.getName());
        if (DateUtil.getYearDate(System.currentTimeMillis()).equals(mPreferenceUtil.getString(attendanceData.get_id(),""))) {
            holder.updateNote.setVisibility(View.VISIBLE);
        } else {
            holder.updateNote.setVisibility(View.GONE);
        }

        holder.editRule.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mListener.editRule(attendanceData);
            }
        });

        holder.editSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.editSubClass(attendanceData);
            }
        });

        if (attendanceData.getFormType() == 1){
            holder.editSub.setVisibility(View.VISIBLE);
            holder.startTime.setVisibility(View.GONE);
            holder.endTime.setVisibility(View.GONE);
            holder.workDate.setText("排班考勤");
        }else{
            holder.editSub.setVisibility(View.GONE);
            holder.startTime.setVisibility(View.VISIBLE);
            holder.endTime.setVisibility(View.VISIBLE);
            holder.startTime.setText("上班：" + attendanceData.getStartWorkTime());
            holder.endTime.setText("下班：" + attendanceData.getEndWorkTime());
            if (!StringUtil.isEmpty(attendanceData.getWorkDate())){
                holder.workDate.setText(setWorkDate(attendanceData.getWorkDate()));
            }
        }

        return convertView;
    }

    class ViewHolder {
        TextView name, workDate, startTime, endTime, updateNote;
        TextView editRule,editSub;

    }

    private String setWorkDate(String workDate) {
        if ("1,2,3,4,5".equals(workDate)) {
            return "工作日";
        } else {
            StringBuilder sb = new StringBuilder();
            String weeks[] = workDate.split(",");
            for (String week : weeks) {
                if ("1".equals(week)) {
                    sb.append("周一").append(",");
                } else if ("2".equals(week)) {
                    sb.append("周二").append(",");
                } else if ("3".equals(week)) {
                    sb.append("周三").append(",");
                } else if ("4".equals(week)) {
                    sb.append("周四").append(",");
                } else if ("5".equals(week)) {
                    sb.append("周五").append(",");
                } else if ("6".equals(week)) {
                    sb.append("周六").append(",");
                } else if ("7".equals(week)) {
                    sb.append("周日").append(",");
                }
            }
            return sb.toString().substring(0, sb.toString().length() - 1);
        }

    }

    public interface AttendanceEditListener{
        void editRule(AttendanceData attendanceData);
        void editSubClass(AttendanceData attendanceData);
    }

    public void setEditListener(AttendanceEditListener listener){
        this.mListener = listener;
    }
}
